package com.leetcode.题库;

/**
 * @author 覃国强
 * @date 2022-03-03 15:03
 */
public class 题库_0283_移动零 {

  static
  class Solution {
    public void moveZeroes(int[] nums) {
      bubbleSort(nums);
    }

    private void bubbleSort(int[] nums) {
      int lastSwapIndex = nums.length - 1;
      while (lastSwapIndex != -1) {
        int currentSwapIndex = -1;
        for (int i = 0; i < lastSwapIndex; ++i) {
          if (needSwap(nums[i], nums[i + 1])) {
            swap(nums, i, i + 1);
            currentSwapIndex = i;
          }
        }
        lastSwapIndex = currentSwapIndex;
      }
    }

    private boolean needSwap(int a, int b) {
      return a == 0 && b != 0;
    }

    private void swap(int[] nums, int i, int j) {
      int temp = nums[i];
      nums[i] = nums[j];
      nums[j] = temp;
    }

  }

}
